package arbre.utils;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;

public class Noeud implements  Serializable{

	private Noeud l;
	private Noeud r;
//	private Noeud pred;
	private ContentNode c;
	private int d;
	
	public Noeud(){
		
	}

	public Noeud(Noeud l, Noeud r, Noeud pred, ContentNode contenu, int dir){
		this.l = l ;
		this.r = r ;
//		this.pred = pred;
		this.c = contenu;
		d=dir;
	
	}

	

	 
//	public Noeud getPred() {
//		return this.pred;
//	}
//
//	 
//	public void setPred(Noeud n) {
//		this.pred = n;
//	}

	 
	
	 
	public String toString() {
		StringBuffer res = new StringBuffer("");
		res.append("<node dir=\""+changeDirtoXml(d)+"\">\n");
		res.append(c.toString()+"");
		if(l != null){
			
			res.append(l.toString());
			
		}
		if(r != null){
			
			res.append(r.toString()+"");
			
		}

		res.append("</node>\n");

		return res.toString();
	}
	
	public void save(FileWriter fw){
		try {
			fw.write("<node dir=\""+changeDirtoXml(d)+"\">\n");
			fw.write(c.toString()+"");
			if(l != null){
				
				l.save(fw);
				
			}
			if(r != null){
				
				r.save(fw);
				
			}
			fw.write("</node>\n");
			
			System.out.println("le node : "+c.toString()+" a save");
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}

	private String changeDirtoXml(int direction2) {
		switch (direction2) {
		case -100:
			return "left";
		case 1:
			return "right";
		case -1:
			return "left";
		case 100:
			return "right";
		}
		return "Defaut";
	}

	

	

	 
	
	public int taille() {
		int res=  1;
		if(l != null){
			res += l.taille();
		}
		if(r!=null){
			res += r.taille();
		}
		return res;
	}

	public Noeud getL() {
		return l;
	}

	public void setL(Noeud l) {
		this.l = l;
	}

	public Noeud getR() {
		return r;
	}

	public void setR(Noeud r) {
		this.r = r;
	}

	public ContentNode getC() {
		return c;
	}

	public void setC(ContentNode c) {
		this.c = c;
	}

	public int getD() {
		return d;
	}

	public void setD(int d) {
		this.d = d;
	}


}
